<?php

/*
 * convert category level 1
 * input: {puid = 0}
 * output: {cs.id, cs.name, cd.cid, cd.name} //catsource.cid & catsource.name &catdestination.cid
 */

function convert_manufacturerid(){
    global $connect_cameraquansat, $connect_xshop;
    $source_sql = "select * from `sanpham_hang`";
    
    $result = mysql_query($source_sql, $connect_xshop);
    
     if(!$result){
        echo 'Error connect'. mysql_error($connect_xshop); die;
    }
    
    
     //get cid & name of category
    $arr_tmp=array();
    while($object = mysql_fetch_object($result)){
        
        //import to cameraquansat
        //mahang	tenhang	gioithieu
        $mahang = $object->mahang;
        $tenhang = $object->tenhang;
        
        $dest_sql = "insert into `evhri_w_manufacturers` (`id`, `name`,`published`) values ('$mahang', '$tenhang','1')";
        
        $result_order = mysql_query($dest_sql, $connect_cameraquansat);
        if(!$result_order){
            echo 'sommething error!'.mysql_error($connect_cameraquansat); die;
        }
    }
    
    echo 'Successfully!!!';
}

function convertCategory(){
    global $connect_cameraquansat, $connect_xshop;
    $source_sql = "select * from `sanpham_loai` where `parentID` = 13";
    
    $result = mysql_query($source_sql, $connect_xshop);
    
     if(!$result){
        echo 'Error connect'. mysql_error($connect_xshop); die;
    }
    
    
     //get cid & name of category
    $arr_tmp=array();
    while($object = mysql_fetch_object($result)){
        
        //import to cameraquansat
        //mahang	tenhang	gioithieu
        $tenloai = $object->tenloai;
        
        $dest_sql = "insert into `evhri_w_categories` (`parentid`,`name`,`published`) values (0,'$tenloai',1)";
        
        $result_order = mysql_query($dest_sql, $connect_cameraquansat);
        if(!$result_order){
            echo 'sommething error!'.mysql_error($connect_cameraquansat); die;
        }
    }
    
    echo 'Successfully!!!';
}


function convertProduct(){
    global $connect_cameraquansat, $connect_xshop;
    
    //Lấy danh sách ma loại và thứ tự đễ input vào trong table  evhri_w_products
    $arr_inter_maloai = array();
    $source_sql = "select * from `sanpham_loai` where `parentID` = 13";
    $result = mysql_query($source_sql, $connect_xshop);
     if(!$result){
        echo 'Error connect'. mysql_error($connect_xshop); die;
    }
    $i = 1;
    while($object = mysql_fetch_object($result)){
        
        //import to cameraquansat
        //mahang	tenhang	gioithieu
        $maloai = $object->maloai; //thứ tự bd = 1;
        $arr_inter_maloai[$i++] = $maloai;
    }
    
    $q_maloai = "('".implode("', '",$arr_inter_maloai)."')";
    $source_sql = "select * from `sanpham` where `maloai` in $q_maloai";
    
    $result = mysql_query($source_sql, $connect_xshop);
    
     if(!$result){
        echo 'Error connect'. mysql_error($connect_xshop); die;
    }
     //get cid & name of category
    $arr_tmp=array();
    while($object = mysql_fetch_object($result)){
        
        //import to cameraquansat
        /**
         *  masp            ->  machinh
            tensp           ->  name
            gianhap         ->  oldprice
            giaban          ->  newprice
            khuyenmai       ->  khuyen-mai
            ngaybatdau      ->  ngay-bat-dau
            ngayketthuc     ->  ngay-ket-thuc
            thongtinkhuyenmai-> thong-tin-khuyen-mai
            trangthai       ->  status
            tinhtrang       ->  tinh-trang
            maloai          ->  catid
            mahang          ->  manufacturerid
            xuatsu          ->  xuat-xu
            mota            ->  description
            thongso         ->  intro
            baohanh         ->  baohanh
            phukien         ->  phu-kien
         */
       $masp             = mysql_real_escape_string($object->masp             );
        $tensp            = mysql_real_escape_string($object->tensp            );
        $gianhap          = mysql_real_escape_string($object->gianhap          );
        $giaban           = mysql_real_escape_string($object->giaban           );
        $khuyenmai        = mysql_real_escape_string($object->khuyenmai        );
        $ngaybatdau       = mysql_real_escape_string($object->ngaybatdau       );
        $ngayketthuc      = mysql_real_escape_string($object->ngayketthuc      );
        $thongtinkhuyenmai= mysql_real_escape_string($object->thongtinkhuyenmai);
        $trangthai        = mysql_real_escape_string($object->trangthai        );
        $tinhtrang        = mysql_real_escape_string($object->tinhtrang        );
        $maloai           = mysql_real_escape_string($object->maloai           );
        $mahang           = mysql_real_escape_string($object->mahang           );
        $xuatsu           = mysql_real_escape_string($object->xuatsu           );
        $mota             = mysql_real_escape_string($object->mota             );
        $thongso          = mysql_real_escape_string($object->thongso          );
        $baohanh          = mysql_real_escape_string($object->baohanh          );
        $phukien          = mysql_real_escape_string($object->phukien          );
        $image            = mysql_real_escape_string($object->hinhanh          );
        
        
        
        //thay đỗi dữ liệu trước khi import vào database
        
        //xuất xứ
        switch ($xuatsu){
            case 'VietNam':
                $xuatsu = 0;
                break;
            case 'Japan':
                $xuatsu = 1;
                break;
            case 'China':
                $xuatsu = 2;
                break;
            default:
                $xuatsu = '';
                break;
        }
        
        //maloai
        $maloai = end(array_keys($arr_inter_maloai,$maloai));
        
        $dest_sql = "insert into `evhri_w_products` (`machinh`,`name`,`oldprice`,`newprice`,`khuyen-mai`,`ngay-bat-dau`,`ngay-ket-thuc`,`thong-tin-khuyen-mai`,
                                                      `status`,`tinh-trang`,`catid`,`manufacturerid`,`xuat-xu`,`description`,`intro`,`bao-hanh`,`phu-kien`,`published`) 
                     values ('$masp','$tensp','$gianhap','$giaban','$khuyenmai','$ngaybatdau','$ngayketthuc','$thongtinkhuyenmai','$trangthai','$tinhtrang','$maloai'
                            ,'$mahang','$xuatsu','$mota','$thongso','$baohanh','$phukien','1')";
        
        $result_order = mysql_query($dest_sql, $connect_cameraquansat);
        
        
        
        if(!$result_order){
            echo 'sommething error!'.mysql_error($connect_cameraquansat); die;
        }
        
        //thực hiện việc insert image vào đây luôn.
        $product_id = mysql_insert_id($connect_cameraquansat);
//        $path_file = 'd:\\xampp\\htdocs\\cameraquansat.com.vn\\attachment\\products\\'.$image;
//        if(!file_exists($path_file)){
//            debug($path_file.'  do not exists'); 
//            /**
//             * Hyundai DHY 20KSE.jpg
//             * Hyundai DHY 12KSE.jpg
//             * Hyundai DHY 22KSEm.jpg
//             */
//        }
        
        //thay doi du lieu cho $image
        //$image_path = get_image_path($product_id);
        
        //$path = $image_path.$image;
        
        $query = "
            INSERT INTO `evhri_w_images` (`proid`,
                                           `filename`,
                                           `ordering`,
                                           `isdefault`,
                                           `published`)
                                            VALUES
                                            (
                                              '$product_id',
                                              '$image',
                                              '0',
                                              '1',
                                              '1'
                                            )
            ";
        $tmp = mysql_query($query, $connect_cameraquansat);
        if(!$tmp){
            echo 'Insert query wrong query '.mysql_error($connect_cameraquansat);
        }
    }
    
    echo 'Successfully!!!';
}


function get_image_path($product_id){
    global $connect_cameraquansat, $connect_xshop;
    $query = "select catid from `evhri_w_products` where id = '$product_id' limit 1";
    $result = mysql_query($query, $connect_cameraquansat);
    
     if(!$result){
        echo 'Error connect'. mysql_error($connect_cameraquansat); die;
    }
     //get cid & name of category
    $maloai = mysql_result($result, 0);
    
    $data = get_data_tblcategories($maloai);
    
    return str_to_alias($data->name).'-'.$data->id.'/';
    
}

function get_data_tblcategories($catid){
    global $connect_cameraquansat, $connect_xshop;
    $query = "select * from `evhri_w_categories` where id = '$catid' limit 1";
    $result = mysql_query($query, $connect_cameraquansat);
    
     if(!$result){
        echo 'Error connect'. mysql_error($connect_cameraquansat); die;
    }
     //get cid & name of category
    return mysql_fetch_object($result);
}

function str_to_alias($string, $replace = '_') {
    $trans = array(
        'à' => 'a', 'á' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a',
        'ă' => 'a', 'ằ' => 'a', 'ắ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a',
        'â' => 'a', 'ầ' => 'a', 'ấ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a',
        'À' => 'a', 'Á' => 'a', 'Ả' => 'a', 'Ã' => 'a', 'Ạ' => 'a',
        'Ă' => 'a', 'Ằ' => 'a', 'Ắ' => 'a', 'Ẳ' => 'a', 'Ẵ' => 'a', 'Ặ' => 'a',
        'Â' => 'a', 'Ầ' => 'a', 'Ấ' => 'a', 'Ẩ' => 'a', 'Ẫ' => 'a', 'Ậ' => 'a',
        'đ' => 'd', 'Đ' => 'd',
        'è' => 'e', 'é' => 'e', 'ẻ' => 'e', 'ẽ' => 'e', 'ẹ' => 'e',
        'ê' => 'e', 'ề' => 'e', 'ế' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e',
        'È' => 'e', 'É' => 'e', 'Ẻ' => 'e', 'Ẽ' => 'e', 'Ẹ' => 'e',
        'Ê' => 'e', 'Ề' => 'e', 'Ế' => 'e', 'Ể' => 'e', 'Ễ' => 'e', 'Ệ' => 'e',
        'ì' => 'i', 'í' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i',
        'Ì' => 'i', 'Í' => 'i', 'Ỉ' => 'i', 'Ĩ' => 'i', 'Ị' => 'i',
        'ò' => 'o', 'ó' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o',
        'ô' => 'o', 'ồ' => 'o', 'ố' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o',
        'ơ' => 'o', 'ờ' => 'o', 'ớ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o',
        'Ò' => 'o', 'Ó' => 'o', 'Ỏ' => 'o', 'Õ' => 'o', 'Ọ' => 'o',
        'Ô' => 'o', 'Ồ' => 'o', 'Ố' => 'o', 'Ổ' => 'o', 'Ỗ' => 'o', 'Ộ' => 'o',
        'Ơ' => 'o', 'Ờ' => 'o', 'Ớ' => 'o', 'Ở' => 'o', 'Ỡ' => 'o', 'Ợ' => 'o',
        'ù' => 'u', 'ú' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u',
        'ư' => 'u', 'ừ' => 'u', 'ứ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u',
        'Ù' => 'u', 'Ú' => 'u', 'Ủ' => 'u', 'Ũ' => 'u', 'Ụ' => 'u',
        'Ư' => 'u', 'Ừ' => 'u', 'Ứ' => 'u', 'Ử' => 'u', 'Ữ' => 'u', 'Ự' => 'u',
        'ỳ' => 'y', 'ý' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y',
        'Y' => 'y', 'Ỳ' => 'y', 'Ý' => 'y', 'Ỷ' => 'y', 'Ỹ' => 'y', 'Ỵ' => 'y'
    );
    $str = str_replace(' ', $replace, strtr($string, $trans));
    return str_replace('%','',$str);
}